package com.llh.controller.processdata;

import com.llh.bean.FinaIndicator;
import com.llh.bean.FinalData;
import com.llh.bean.MiddleData;
import com.llh.controller.processdata.computVo.BasicVo;
import com.llh.controller.processdata.computVo.Daily_hz;
import com.llh.controller.processdata.computVo.PrimaryKey;
import com.llh.mapper.*;
import com.llh.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ComputeBasic {
    static Logger logger = LogManager.getLogger(ComputeDaily.class.getName());

    public static void computeBasic() throws Exception{
        SqlSession sqlSession = null;
        sqlSession = SqlSessionUtil.getSqlSesion();
        FinalDataMapper mapper = sqlSession.getMapper(FinalDataMapper.class);
        List<FinalData> all = mapper.findAll();
        sqlSession.close();
        File in = new File("C:\\Users\\llh\\Desktop\\股票基本信息表头导表.xlsx");
        File out = new File("C:\\Users\\llh\\Desktop\\股票基本信息导表.xlsx");
        writeExcel(in,out,1,all);

    }

    //创建表头
    public static void creatExcel() throws Exception {
        XSSFWorkbook workbook1 = new XSSFWorkbook();
        SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(workbook1, 100);
        SXSSFSheet sheet = sxssfWorkbook.createSheet("基本数据");
        SXSSFRow row = sheet.createRow(0);
        SXSSFCell cell0_0 = row.createCell(0);
        cell0_0.setCellValue("股票代码");
        SXSSFCell cell0_1 = row.createCell(1);
        cell0_1.setCellValue("名称");
        SXSSFCell cell0_2 = row.createCell(2);
        cell0_2.setCellValue("市场类型");
        SXSSFCell cell0_3 = row.createCell(3);
        cell0_3.setCellValue("所属行业");
        SXSSFCell cell0_4 = row.createCell(4);
        cell0_4.setCellValue("地域");
        SXSSFCell cell0_5 = row.createCell(5);
        cell0_5.setCellValue("解禁日");
        SXSSFCell cell0_6 = row.createCell(6);
        cell0_6.setCellValue("解禁日上一季度报告期");
        SXSSFCell cell0_7 = row.createCell(7);
        cell0_7.setCellValue("报告期去年同期");
        SXSSFCell cell0_8 = row.createCell(8);
        cell0_8.setCellValue("净资产收益率(上一季度)");
        SXSSFCell cell0_9 = row.createCell(9);
        cell0_9.setCellValue("资产负债率(上一季度)");
        SXSSFCell cell0_10 = row.createCell(10);
        cell0_10.setCellValue("同比增长率:营业收入");
        SXSSFCell cell0_11 = row.createCell(11);
        cell0_11.setCellValue("同比增长率:归属母公司股东的净利润");
        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\llh\\Desktop\\股票基本信息表头导表.xlsx"));
        sxssfWorkbook.write(fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        sxssfWorkbook.close();
        System.out.println("结束");
    }

    //写数据
    public static void writeExcel(File in, File out, int startRow,  List<FinalData> finalDataList) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        FileInputStream fileInputStream = null;
        XSSFWorkbook workbook1 = null;
        SXSSFWorkbook sxssfWorkbook = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(in);
            workbook1 = new XSSFWorkbook(fileInputStream);
            sxssfWorkbook = new SXSSFWorkbook(workbook1, 100);
            SXSSFSheet sheet = sxssfWorkbook.getSheetAt(0);
            if(finalDataList == null || finalDataList.size()==0){
                System.out.println("没要要导的数据结束");
                return;
            }
            for (FinalData finalData : finalDataList) {
                SXSSFRow row = sheet.createRow(startRow);
                SXSSFCell cell0_0 = row.createCell(0);
                cell0_0.setCellValue(finalData.getS_code());
                SXSSFCell cell0_1 = row.createCell(1);
                cell0_1.setCellValue(finalData.getS_name());
                SXSSFCell cell0_3 = row.createCell(2);
                cell0_3.setCellValue(finalData.getMarket());
                SXSSFCell cell0_4 = row.createCell(3);
                cell0_4.setCellValue(finalData.getIndustry());
                SXSSFCell cell0_5 = row.createCell(4);
                cell0_5.setCellValue(finalData.getArea());
                SXSSFCell cell0_6 = row.createCell(5);
                cell0_6.setCellValue(finalData.getOpen_day());
                SXSSFCell cell0_7 = row.createCell(6);
                cell0_7.setCellValue(finalData.getEnd_date());
                SXSSFCell cell0_8 = row.createCell(7);
                cell0_8.setCellValue(finalData.getPreyear_end_date());
                SXSSFCell cell0_2 = row.createCell(8);
                cell0_2.setCellValue(finalData.getRoe());
                SXSSFCell cell0_9 = row.createCell(9);
                //小数位原格式
                cell0_9.setCellValue(finalData.getDebt_to_assets());
                SXSSFCell cell0_10 = row.createCell(10);
                //小数位原格式
                cell0_10.setCellValue(finalData.getYoy_sales());
                SXSSFCell cell0_11 = row.createCell(11);
                //小数位原格式
                cell0_11.setCellValue(finalData.getYoy_dedu_np());

/*//                cell.setCellValue(new BigDecimal(data.toString()).setScale(4, BigDecimal.ROUND_HALF_UP).toString());

                    //导真实数据
                    Double data = daily_hzs.get(i).getData();
                    if(data==null){
                        cell.setCellValue("");
                    }else{
//                        cell.setCellValue(daily_hzs.get(i).getData());
                        cell.setCellValue(new BigDecimal(data.toString()).setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                    }
                }*/
                System.out.println("插入第" + startRow + "行数据完成");
                startRow++;
            }
            fileOutputStream = new FileOutputStream(out);
            sxssfWorkbook.write(fileOutputStream);
            fileOutputStream.flush();


        } finally {
            fileInputStream.close();
            fileOutputStream.close();
            sxssfWorkbook.close();
        }
    }

    public static void main(String[] args) throws Exception {
  /*      creatExcel();
        System.out.println("表头创建成功");*/
        computeBasic();
        System.out.println("导表结束");
    }
}
